class PenjualanSubsController < ApplicationController
  # GET /penjualan_subs
  # GET /penjualan_subs.json
  def index
    @penjualan_subs = PenjualanSub.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @penjualan_subs }
    end
  end

  # GET /penjualan_subs/1
  # GET /penjualan_subs/1.json
  def show
    @penjualan_sub = PenjualanSub.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @penjualan_sub }
    end
  end

  # GET /penjualan_subs/new
  # GET /penjualan_subs/new.json
  def new
    @penjualan_sub = PenjualanSub.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @penjualan_sub }
    end
  end

  # GET /penjualan_subs/1/edit
  def edit
    @penjualan_sub = PenjualanSub.find(params[:id])
  end

  # POST /penjualan_subs
  # POST /penjualan_subs.json
  def create
    @penjualan_sub = PenjualanSub.new(params[:penjualan_sub])

    respond_to do |format|
      if @penjualan_sub.save
        format.html { redirect_to @penjualan_sub, notice: 'Penjualan sub was successfully created.' }
        format.json { render json: @penjualan_sub, status: :created, location: @penjualan_sub }
      else
        format.html { render action: "new" }
        format.json { render json: @penjualan_sub.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /penjualan_subs/1
  # PUT /penjualan_subs/1.json
  def update
    @penjualan_sub = PenjualanSub.find(params[:id])

    respond_to do |format|
      if @penjualan_sub.update_attributes(params[:penjualan_sub])
        format.html { redirect_to @penjualan_sub, notice: 'Penjualan sub was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @penjualan_sub.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /penjualan_subs/1
  # DELETE /penjualan_subs/1.json
  def destroy
    @penjualan_sub = PenjualanSub.find(params[:id])
    @penjualan_sub.destroy

    respond_to do |format|
      format.html { redirect_to penjualan_subs_url }
      format.json { head :ok }
    end
  end
  
  def add_row
    
    if params[:barcodescan]
      @split = params[:barcodescan].split('*')
      
      if @split[1].to_s == '' #jika user commit barcode saja berarti jumlah barang 1 pada barcode tsb
        @barcode = @split[0].to_s
        @number = 1
      else
        @number = @split[0].to_i
        @barcode = @split[1].to_s
      end
      
      @barang = Barang.where('KodeBrg =?', @barcode).first
      
      if @barang  
        @penjualan_sub = PenjualanSub.new
        @penjualan_sub.penjualanid = params[:penjualanid]
        @penjualan_sub.qty = @number
        @penjualan_sub.barangid = @barang.id
        @penjualan_sub.hargapokok = @number * @barang.HargaPokok
        @penjualan_sub.hargajual = @number* @barang.HargaEceran
        @penjualan_sub.total = @number * @barang.HargaEceran
        @penjualan_sub.status = 0
        @penjualan_sub.save
        
        @total_penjualan = PenjualanSub.where('penjualanid =?',params[:penjualanid]).sum("total")
      else
        @report = 'barang tidak ada'
      end 
    end 
    
    respond_to do |format|
      format.js { render :layout => false }
    end
  end
  
end
